package com.ljf.eslab.lab.service.impl;

import com.ljf.eslab.lab.dto.ReserInfoDTO;
import com.ljf.eslab.lab.dto.TimeListAndLidsDTO;
import com.ljf.eslab.lab.dto.TimeListDTO;
import com.ljf.eslab.lab.vo.ReserDetailVo;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljf.common.utils.PageUtils;
import com.ljf.common.utils.Query;

import com.ljf.eslab.lab.dao.ReservationDao;
import com.ljf.eslab.lab.entity.ReservationEntity;
import com.ljf.eslab.lab.service.ReservationService;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("reservationService")
public class ReservationServiceImpl extends ServiceImpl<ReservationDao, ReservationEntity> implements ReservationService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ReservationEntity> page = this.page(
                new Query<ReservationEntity>().getPage(params),
                new QueryWrapper<ReservationEntity>()
        );

        return new PageUtils(page);
    }

    public List<ReserInfoDTO> listByTime(List<Integer> week, List<Integer> day, List<Integer> time) {
        return this.baseMapper.getReserInfoByTime(new TimeListDTO(week,day,time));
    }
    public PageUtils pageByTime(IPage<ReservationEntity> page,List<Integer> week, List<Integer> day, List<Integer> time) {
        IPage<ReserDetailVo> pageByTime = this.baseMapper.getReserPageByTime(page, new TimeListDTO(week, day, time));
        return new PageUtils(pageByTime);
    }

    @Override
    public PageUtils pageByLids(IPage<ReserDetailVo> page, List<Integer> ids) {
        IPage<ReserDetailVo> pageByIds = this.baseMapper.getReserDetailPageByLids(page,ids);
        return new PageUtils(pageByIds);
    }

    @Override
    public PageUtils pageByCids(IPage <ReservationEntity> page, List<Integer> cids) {
        IPage<ReserDetailVo> pageByCIds = this.baseMapper.getReserDetailByCids(page,cids);
        return new PageUtils(pageByCIds);
    }

    @Override
    public PageUtils pageByTimeAndIds(IPage<ReserDetailVo> params, List<Integer> weeks, List<Integer> days, List<Integer> times, List<Integer> labIds) {
        TimeListAndLidsDTO dto = new TimeListAndLidsDTO(weeks, days, times, labIds);
        IPage<ReserDetailVo> page = this.baseMapper.getPageByTimeAndLids(params,dto);
        return new PageUtils(page);
    }

    @Override
    public void updateCidByIds(List<Integer> ids, Integer courseId) {
        this.baseMapper.updateCidByIds(ids,courseId);
    }


    @Override
    public List<ReserInfoDTO> listByTimeAndIds(List<Integer> weeks, List<Integer> days, List<Integer> times, List<Integer> labIds) {
        HashSet<Integer> labs = new HashSet<>(labIds);
        List<ReserInfoDTO> reserInfoList = this.baseMapper.getReserInfoByTime(new TimeListDTO(weeks,days,times));
        return reserInfoList.stream()
                .filter(reserInfoDTO -> reserInfoDTO.getCount() >= weeks.size() * days.size() * times.size())
                .filter(reserInfoDTO -> labs.contains(reserInfoDTO.getLabId()))
                .collect(Collectors.toList());
    }

}